#include "CollectQuest.h"


CollectQuest::CollectQuest()
{
	TimeLimit = 0.0f;
	InitialTimeLimit = 0.0f;
}

CollectQuest::~CollectQuest()
{

}

void CollectQuest::Intialise()
{
	//reset orbs and bools
	for(std::vector<unsigned int>::iterator it = OrbIDs.begin(); it < OrbIDs.end(); it++)
	{
		if(OrbInstance)
		{
			//set orbs to be active
			OrbInstance->SetIsActive((*it),true);
		}
	}

	TimeLimit = InitialTimeLimit;

	SetIsCompleted(false);
	SetFailed(false);
}

void CollectQuest::Update(float delta,XMFLOAT3 playerpos)
{
	if(!GetFailed() && !GetIsCompleted())
	{
		//loop through orbs and check is they have all been collected
		bool collected = true;

		for(std::vector<unsigned int>::iterator it = OrbIDs.begin(); it < OrbIDs.end(); it++)
		{
			if(OrbInstance)
			{
				//check is orb is active
				if(OrbInstance->GetIsActive((*it)))
				{
					collected = false;
				}
			}
		}

		if(collected)
		{
			SetIsCompleted(true);
		}

		//lower time limit based on time since last frame
		TimeLimit -= delta;

		//if the time limt has run out and the quest has not already been completed
		if(TimeLimit <= 0 && !GetIsCompleted())
		{
			//set quest to failed
			SetFailed(true);

			for(std::vector<unsigned int>::iterator it = OrbIDs.begin(); it < OrbIDs.end(); it++)
			{
				if(OrbInstance)
				{
					//check is orb is active
					OrbInstance->SetIsActive((*it),false);
				}
			}
		}
	}
}

void CollectQuest::SetCollectables(InstancedOrbs* instance,std::vector<unsigned int> id,float time)
{
	
	OrbIDs = id;
	OrbInstance = instance;

	//set all orbs to be inactive
	for(std::vector<unsigned int>::iterator it = OrbIDs.begin(); it < OrbIDs.end(); it++)
	{
		if(OrbInstance)
		{
			//set orbs to be inactive
			OrbInstance->SetIsActive((*it), false);
		}
		
		//Next itteration.
		
	}

	InitialTimeLimit = time;
}